<?php
/**
 * Created by 绵阳人维网络科技有限公司.
 * User: 罗东华
 * Date: 2016/5/12
 * Time: 14:44
 */
namespace Admin\Controller;
class FieldController extends AdminBase{
    private  $fields;
    private  $modelfield;

    //初始化
    public function _initialize() {
        parent::_initialize();
        $this->modelfield=D('ModelField');
        //字段类型存放目录
        $this->fields = MODULE_PATH . 'Fields/';
    }


    //字段属性配置
    public function field_setting() {
        //字段类型
        $fieldtype = I('get.fieldtype');
        $fiepath = $this->fields . $fieldtype . '/';
        //载入对应字段配置文件 config.inc.php

        include $fiepath . 'config.inc.php';
        ob_start();
        include $fiepath . "field_add_form.inc.php";
        $data_setting = ob_get_contents();
        ob_end_clean();
        $settings = array('field_basic_table' => $field_basic_table, 'field_minlength' => $field_minlength, 'field_maxlength' => $field_maxlength, 'field_allow_search' => $field_allow_search, 'field_allow_fulltext' => $field_allow_fulltext, 'field_allow_isunique' => $field_allow_isunique, 'setting' => $data_setting);
        echo json_encode($settings);
        return true;
    }

    //编辑字段
    public function edit(){
        $id=I('get.id','','int');
        $mid=I('get.mid','','int');
        $where['fieldid']=$id;
        if(IS_POST && $mid){
            $post = $_POST;
            if (empty($post)) {
                $this->error('数据不能为空！');
            }
            if($id){
                $this->ThisReturn(D('ModelField')->editField($post, $id));
            }else{
                $this->ThisReturn(D('ModelField')->addField($post));
            }

        }

        $fieldData=M('model_field')->where($where)->find();
        //字段扩展配置
        $setting=unserialize($fieldData['setting']);

        if (empty($fieldData) && $id) {
            $this->error('该字段信息不存在！');
        }
        //字段路径
        $fiepath = $this->fields . "{$fieldData['formtype']}/";
        //======获取字段类型的表单编辑界面===========

        //打开缓冲区
        ob_start();
        include $fiepath . 'field_edit_form.inc.php';
        $form_data = ob_get_contents();
        //关闭缓冲
        ob_end_clean();
        //======获取字段类型的表单编辑界面===END====
        //字段类型过滤
        $all_field = array();
        foreach ($this->modelfield->getFieldTypeList() as $formtype => $name) {
            if (!$this->modelfield->isEditField($formtype)) {
                continue;
            }
            $all_field[$formtype] = $name;
        }
        //不允许删除的字段，这些字段讲不会在字段添加处显示
        $this->assign("not_allow_fields", $this->modelfield->not_allow_fields);
        //允许添加但必须唯一的字段
        $this->assign("unique_fields", $this->modelfield->unique_fields);
        //禁止被禁用的字段列表
        $this->assign("forbid_fields", $this->modelfield->forbid_fields);
        //禁止被删除的字段列表
        $this->assign("forbid_delete", $this->modelfield->forbid_delete);
        //可以追加 JS和CSS 的字段
        $this->assign("att_css_js", $this->modelfield->att_css_js);
        //允许使用的字段类型
        $this->assign("all_field", $all_field);
        //当前字段是否允许编辑
        $this->assign('isEditField', $this->modelfield->isEditField($fieldData['field']));
        //附加属性
        $this->assign("form_data", $form_data);
        $this->assign('setting',$setting);
        $this->assign('info',$fieldData);
        $MODEL_FIELD=C('MODEL_FIELD_TYPE');
        $this->assign('MODEL_FIELD',$MODEL_FIELD);
        $this->assign('mid',$mid);

        $this->display();
    }
    //检查模型字段重复
    public function check(){

        if(IS_AJAX){
            $mid=I('get.mid','','int');
            $fid=I('get.fid','','int');
            if(!$mid){
                $this->ajaxReturn(validreturn(-1,'','没有模型id'));
            }
            $where['modelid']=$mid;
            if($fid){
                $where['fieldid']=array('neq',$fid);
            }

            $this->ajaxReturn(field_check('model_field',$where));


        }


    }
    /*删除字段*/
    public function del($fid){
        if(IS_AJAX){
            if(!$fid){
                $this->ajaxReturn(validreturn(false));
            }
            $this->ajaxreturn(validreturn(D('ModelField')->deleteField($fid)));
        }

    }
    /*字段禁用*/
    public function dis($fid,$dis){

        if(IS_AJAX && $fid){
            $dis=$dis==0?1:0;
            $where['fieldid']=$fid;
            $res=M('model_field')->where($where)->save(array('disabled'=>$dis));
            $this->ajaxreturn(validreturn($res,'操作成功!','操作失败!'));
        }

    }


}